/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Author:
 *     coolair.loveU2@gmail.com
 *******************************************************************************/

package org.skyfree.ghyll.tcard.core.internal;

import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.core.runtime.CoreException;
import org.skyfree.ghyll.tcard.core.IStoragePath;
import org.skyfree.ghyll.tcard.core.ITProject;
import org.skyfree.ghyll.tcard.core.ITProjectDescription;
import org.skyfree.ghyll.tcard.core.ITWorkspace;

/**
 * This class ...
 */

public class TWorkspace extends AbstractTResource implements ITWorkspace
{
	HashMap<String, ITProject> cacheProject;
	
	
	public TWorkspace(IStoragePath path)
	{
		super(path);
	}

	@Override
	public ITProject getProject(String name)
	{
		ITProject value = this.cacheProject.get(name);
		if(value==null)
		{
			TProject project = new TProject(this.path.append(name),name);
			project.workspace = this;
			return project;
		}
		else
		{
			try
			{
				value.load();
			} catch (CoreException e)
			{
				this.log(e);
			}
			return value;
		}
	}

	@Override
	public ITProject[] getProjects()
	{
		ITProject[] re = new ITProject[0];
		return (ITProject[]) this.cacheProject.values().toArray(re);
	}

	@Override
	public ITProjectDescription newProjectDescription(String projectName)
	{
		TProjectDescription re = new TProjectDescription(this.path.append(projectName));
		re.name = projectName;
		return re;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Class adapter)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void load() throws CoreException
	{
		if(loaded)return;
		this.cacheProject = new HashMap<String, ITProject>();
		String[] projectNames = this.provider.load(this);
		for(int i=0;i<projectNames.length;i++)
		{
			TProject oneProject = new TProject(this.path.append(projectNames[i]),projectNames[i]);
			oneProject.workspace = this;
			oneProject.exist = true;
			oneProject.load();
			this.cacheProject.put(projectNames[i], oneProject);
		}
		this.loaded = true;
		
	}

	/**
	 * this method is orphaned
	 */
	@Override
	public void save() throws CoreException
	{
		Iterator<ITProject> it = this.cacheProject.values().iterator();
		while(it.hasNext())
		{
			it.next().save();
		}
		this.dirty = false;
	}

	@Override
	public void reload() throws CoreException
	{
		Iterator<ITProject> it = this.cacheProject.values().iterator();
		while(it.hasNext())
		{
			it.next().reload();
		}
		this.dirty = false;
	}
	@Override
	public boolean exist(String name)
	{
		if(!this.loaded)
		{
			try
			{
				this.load();
			}catch(CoreException e)
			{
				this.log(e);
			}
		}
		return this.cacheProject.get(name)!=null;
	}
	
	void add2Workspace(ITProject project)
	{
		cacheProject.put(project.getName(), project);
	}

	
	


	
}
